<template>
	<el-container>
	  <el-header class="header-wrapped">
		  <div class="header-content">
			  <h3>通用后台管理系统</h3>
			  <span class="welcome">欢迎您的登录</span>
		  </div>
	  </el-header>
	  <el-main>
		  <div class="login-warpped">
			   <el-card class="box-card">
					 <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
					    <el-tab-pane label="登录" name="first">
							<el-form class="login-form">
								<el-form-item label="账号">
									<el-input v-model="loginData.account" placeholder="请输入账号"></el-input>
								</el-form-item>
								<el-form-item label="密码">
									<el-input v-model="loginData.password" placeholder="请输入密码"></el-input>
								</el-form-item>
								<!-- 底部外壳 -->
								<div class="footer-wrapped">
									<div class="forget-password">
										<span class="forget-password-button" @click="openForget">忘记密码</span>
									</div>
									<div class="footer-button">
										<el-button type="primary" @click="Login">登录</el-button>
									</div>
									<div class="footer-go-register">
										还没有账号？<span class="go-register">马上注册</span>
									</div>
								</div>
							</el-form>
						</el-tab-pane>
					    <el-tab-pane label="注册" name="second">
							<el-form class="login-form">
								<el-form-item label="账号">
									<el-input v-model="registerData.account" placeholder="账号长度为6-12位"></el-input>
								</el-form-item>
								<el-form-item label="密码">
									<el-input v-model="registerData.password" placeholder="密码长度需为6-12位含字母数字"></el-input>
								</el-form-item>
								<el-form-item label="确认密码">
									<el-input v-model="registerData.repassword" placeholder="请再次输入密码"></el-input>
								</el-form-item>
								<div class="footer-button">
									<el-button type="primary" @click="Register">注册</el-button>
								</div>
							</el-form>
						</el-tab-pane>
					  </el-tabs>
			    </el-card>
		  </div>
	  </el-main>
	  <el-footer class="footer-wrapped">
		  <div class="footer-content">
			  <div class="title">
				  <span>阿里云博客专家</span>
				  <span>阿里云博客专家</span>
				  <span>阿里云博客专家</span>
				  <span>阿里云博客专家</span>
			  </div>
		  </div>
	  </el-footer>
	</el-container>
	<forget ref="forgetP"></forget>
</template>

<script lang="ts" setup>
	import { ref,reactive } from 'vue'
	import { useRouter } from 'vue-router'
	import { ElMessage } from 'element-plus'
	import forget from './components/forget_password.vue'
	import { login, register } from '@/api/login.js'
	import { userUserInfor } from '@/store/userinfor.js'
	const store = userUserInfor();
	const activeName = ref('first')
	const router = useRouter()
	// 表单接口
	interface formData {
		account:number,
		password: string,
		repassword?:string
	}
	// 登录表单数据
	const loginData:formData = reactive({
		account:'123456',
		password:"a123123"
	})
	// 注册表单数据
	const registerData:formData = reactive({
		account:null,
		password:"",
		repassword:"",
	})
	// 登录
	const Login = async () =>{
		const res = await login(loginData);
		const { token } = res.data
		
		console.log(res)
		if(res.data.message == "登录成功"){
			ElMessage({
				message:"登录成功",
				type:"success"
			})
			localStorage.setItem("token",token)
			// 跳转
			router.push("/home")
			const { id } = res.data.results;
			store.userInfor(id);
		}else{
			ElMessage({
				message:res.data.message,
				type:"error"
			})
		}
	}
	// 注册
	const Register = async ()=>{
		if(registerData.password !== registerData.repassword){
			return ElMessage({
				message:"注册失败，请检查密码是否一致",
				type:"error"
			})
		}
		const res = await register(registerData);
		if(res.data.message == "注册账号成功"){
			ElMessage({
				message:"注册成功",
				type:"success"
			})
			activeName.value = "first"
		}else{
			ElMessage({
				message:"注册失败，请检查数据是否正确",
				type:"error"
			})
		}
	}
	
	const forgetP = ref();
	const openForget = ()=>{
		forgetP.value.open();
	}
	
</script>

<style lang="scss" scoped>
	.header-wrapped{
		.header-content{
			width: 1000px;
			margin: 0 auto;
			display:flex;
			justify-content: space-between;
			align-items: center;
			.welcome{
				font-size: 13px;
			}
		}
	}
	.el-main{
		--el-main-padding:0;
		background:url('@/assets/code.jpg') no-repeat no-repeat;
		height: 600px;
		.login-warpped{
			width: 1200px;
			height: 600px;
			margin: 0 auto;
			.box-card{
				width: 350px;
				height:350px;
				float: right;
				position: relative;
				top:14%;
				.login-form{
					.footer-button{
						width: 100%;
						display: flex;
						justify-content: center;
						.el-button{
							width: 100%;
						}
					}
					.footer-wrapped{
						display: flex;
						flex-direction: column;
						.forget-password{
							display: flex;
							justify-content: flex-end;
							margin: 10px 0;
							.forget-password-button{
								font-size: 12px;
								color: #409eff;
								cursor: pointer;
							}
						}
						
						.footer-go-register{
							font-size: 12px;
							margin: 12px 0;
							display: flex;
							justify-content: center;
							.go-register{
								font-size: 12px;
								color: #409eff;
								cursor: pointer;
							}
						}
					}
					
				}
			}
		}
	}
	.footer-wrapped{
		margin-top: 10px;
		.footer-content{
			width: 1000px;
			margin: 0 auto;
			display: flex;
			justify-content: center;
			align-items: center;
			.title{
				color: #666;
			}
			span{
				display: inline-block;
				padding: 0 10px;
				color: #666;
				font-size: 12px;
			}
		}
	}
	:deep(.el-tabs_item){
		color:#333;
		font-size: 18px;
	}
	:deep(.el-input__inner){
		height: 40px;
	}
	:deep(.el-form-item__label){
		height: 40px;
		line-height: 40px;
	}
</style>
